package org.funtester.performance.books.chapter05.section4;

import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.funtester.performance.books.chapter05.sectioin3.NettyWebSocketClient;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * 订阅测试,Netty-WebSocket客户端实现
 */
public class SubcribeTest {

    public static void main(String[] args) throws URISyntaxException, InterruptedException {
        String url = "ws://localhost:12345/websocket";// 服务端地址
        URI uri = new URI(url);// 转换为URI
        List<ChannelPromise> promises = new ArrayList<>();// 存储连接的promise
        for (int i = 0; i < 10000; i++) {
            NettyWebSocketClient nettyWebSocketClient = new NettyWebSocketClient(uri);// 创建客户端
            ChannelPromise connect = nettyWebSocketClient.connect();// 连接
            promises.add(connect);// 存储promise
        }
        for (int i = 0; i < promises.size(); i++) {
            promises.get(i);// 等待 WebSocket 握手成功
        }
        NettyWebSocketClient.channels.writeAndFlush(new TextWebSocketFrame("订阅蔬菜水果价格和数量频道")).sync();// 发送订阅消息
        System.out.println("订阅成功");// 订阅成功
    }

}
